#include<iostream>
#include <string>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
typedef struct node{
    int data;
    struct node *lchild;
    struct node *rchild;
}BTNode;
void PreorderTraversal( BTNode *BT );
BTNode *CreateBT1(int *pre,int *in,int n);
int main(){
	BTNode *b; 
	int n,i;
	cin>>n;
	int pre[30],in[30];
	for(i=0;i<n;i++){
		cin>>pre[i];
	}
	for(i=0;i<n;i++){
		cin>>in[i];
	}
	b=CreateBT1(pre,in,n);
	cout<<"Preorder:";
	PreorderTraversal(b);	
}
BTNode *CreateBT1(int *pre,int *in,int n){
	BTNode *b;
	int r,*p;
	int k;
	if(n<=0) return NULL;
	r=*(pre+n-1);
	
	b=(BTNode*)malloc(sizeof(BTNode));
	
	b->data=r;
	
	for(p=in;p<in+n;p++){
		if(*p==r)
		break;
	}
	k=p-in;
	b->lchild=CreateBT1(pre,in,k);
	b->rchild=CreateBT1(pre+k,p+1,n-k-1);
	return b;
	
}
void PreorderTraversal( BTNode *BT ){
	if(BT){
		cout<<" "<<BT->data;
		PreorderTraversal(BT->lchild);
		PreorderTraversal(BT->rchild);
	}
}

